
//获取应用实例
const util = require('../../utils/util.js')
const app = getApp()

Page({
  
  data: {
    
    userInfo:"",
    motto: 'Hello World',
    queryResult:"",
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userid:wx.getStorageSync("userid")
  },
 
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '登陆',
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  
  getUserInfo1: function(e) {
   wx.getUserProfile({
     desc: '获取头像昵称',
     success :ress =>{
  
    
    app.globalData.userInfo = ress.userInfo
    wx.setStorageSync('userInfo', ress.userInfo)
    this.setData({
      userInfo: ress.userInfo,
      hasUserInfo: true
    })
    
    
    this.login1()
  }
})
  },
  
    /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
    /*if(!wx.getStorageSync('userid')){

        wx.reLaunch({
          url: '/pages/choiceness/index',
        })
    }*/
  },

  login1:function(e){
   
           
         // 调用云函数
        let  userInfo =  this.data.userInfo
        wx.cloud.callFunction({
          name: 'login1',
          data: {
            nickName:userInfo.nickName,
            gender:userInfo.gender,
            language:userInfo.language,
            city:userInfo.city,
            province:userInfo.province,
            country:userInfo.country,
            avatarUrl:userInfo.avatarUrl,
            dateAdd:new Date()
          }
          
          
        }).then(res =>{
          

          if(res.result.user.data[0].isManager == 1){
            app.globalData.openid = res.result.openid
            wx.setStorageSync('openid',  res.result.openid);//存储openid  
            
            wx.setStorageSync('userid',  res.result.user.data[0]._id);//存储openid 
                  wx.reLaunch({
                    url: '../sys/manager/index/index',
                  })
          return
          }
          else if(res.result.user.data[0].isManager == 2){
            app.globalData.openid = res.result.openid
            wx.setStorageSync('openid',  res.result.openid);//存储openid  
           
            wx.setStorageSync('userid',  res.result.user.data[0]._id);//存储openid 
                wx.reLaunch({
                  url: '../sys/manager/index/index',
             })
            return
          }
          else{
            //设置openid
            app.globalData.openid = res.result.openid
            app.globalData.isnewUser = res.result.user.data[0].isnewUser
            wx.setStorageSync('openid',  res.result.openid);//存储openid  
            wx.setStorageSync('userid',  res.result.user.data[0]._id);//存储openid 
            wx.setStorageSync('isnewUser',  res.result.user.data[0].isnewUser);//存储openid 
            //自动加优惠券
            if(res.result.isnewUsr){
               
               app.globalData.isnewUsr=true 
            }
            this.getAddress()
           
            //返回上一页刷新上一页
           
             this.changeParentData()
      
          }
        }
               
        )
  
},
//获取地址
getAddress: function() {

  var that = this;
  //先判断是否已授权
  wx.getSetting({
    success(res) {
    
      if (!res.authSetting['scope.userLocation']) {
        wx.authorize({
          scope: 'scope.userLocation',
          success: function () {
            //授权位置
                wx.chooseLocation	({
                  success: function (res) {
                 
                    app.globalData.map_address=res.address
                    that.setData({
                      map_address : app.globalData.map_address
                    }) 
                  //保存address
                  const db = wx.cloud.database()
                  db.collection('useraddress').where({_openid:app.globalData.openid}).get({
                    success: res1 => {
                    //  console.log(res1)
                      if(res1 && res1.data.length>0){
                         //修改地址
                         db.collection('useraddress').doc(res1.data[0]._id).update({
                          data: {
                            address: res.name+'(' +res.address+')',
                            dateAdd:new Date(),
                          
                          },
                          success: function(res) {
                            console.log(res)
                           
                          }
                        })
                      }else{
                         
                          db.collection('useraddress').add({
                            data: {   
                              address: res.name+'(' +res.address+')',
                              dateAdd:new Date(),
                            },
                            success: function(res) {
                            
                              wx.navigateBack({delta: 1})
                            }
                         })
                      }
                    }
                  })
                  return

                  }
                })
                 //授权位置结束
          },
        })
      }
    }
  })
},
  changeParentData: function () {
      var pages =getCurrentPages();//当前页面栈
    
      if (pages.length >1) {
      var beforePage = pages[pages.length- 2];//获取上一个页面实例对象
     // console.log(beforePage)
      if(  beforePage){
        beforePage.onShow();//触发父页面中的方法
     
        wx.navigateBack({
          delta: 1
          });
      } 
    }
  }
})
